import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useUserStore } from './userStore'
import { insertCartAPI, findNewCartListAPI, delCartAPI } from '@/apis/cart'
export const useCartStore = defineStore(
    'cart',
    () => {
        const userStore = useUserStore()
        const isLogin = computed(() => userStore.userInfo.token)
        // 1.定义state - cartList
        const cartList = ref([])
        // 获取最新购物者列表
        const updateNewList = async () => {
            const res = await findNewCartListAPI()
            cartList.value = res.result
        }
        // 2.定义action - addCart
        const addCart = async (goods) => {
            const { skuId, count } = goods
            if (isLogin.value) {
                await insertCartAPI({ skuId, count })
                updateNewList()
            } else {
                //添加购物车
                //思路：通过匹配传递过来的商品对象中的skuId能不能在chartList中找到，找到就是添加过
                //已添加过  count+1
                // 没有添加过 push
                const item = cartList.value.find(
                    (item) => goods.skuId === item.skuId
                )
                if (item) {
                    item.count++
                } else {
                    cartList.value.push(goods)
                }
                console.log('购物车数据', cartList.value)
            }
        }
        //删除购物车
        const delCart = async (skuId) => {
            if (isLogin.value) {
                await delCartAPI([skuId])
                updateNewList()
            } else {
                //1.找到删除项的下标值，splice
                //2.使用数组的过滤方式 -filter
                const idx = cartList.value.findIndex(
                    (item) => skuId === item.skuId
                )
                cartList.value.splice(idx, 1)
            }
        }
        // 清除购物车
        const clearCart = () => {
            cartList.value = []
        }
        // 单选功能
        const singleCheck = (skuId, selected) => {
            // 通过skuId找到修改项，然后把它的selected的值修改成传过来的selected
            const item = cartList.value.find((item) => item.skuId === skuId)
            item.selected = selected
        }
        // 全选功能
        const allCheck = (selected) => {
            cartList.value.forEach((item) => (item.selected = selected))
        }

        // 计算属性
        // 1. 总数量 所有项的count之和
        const allCount = computed(() =>
            cartList.value.reduce((num, c) => num + c.count, 0)
        )
        // 2总价 所有项的count*price
        const allPrice = computed(() =>
            cartList.value.reduce((num, c) => num + c.count * c.price, 0)
        )
        // 已选择数量
        const selectedCount = computed(() =>
            cartList.value
                .filter((item) => item.selected)
                .reduce((num, c) => num + c.count, 0)
        )
        // 已选择 商品价格合计
        const selectedPrice = computed(() =>
            cartList.value
                .filter((item) => item.selected)
                .reduce((num, c) => num + c.count * c.price, 0)
        )
        // 是否全选
        const isAll = computed(() =>
            cartList.value.every((item) => item.selected)
        )

        return {
            cartList,
            allCount,
            allPrice,
            isAll,
            selectedCount,
            selectedPrice,
            addCart,
            delCart,
            singleCheck,
            allCheck,
            clearCart,
            updateNewList
        }
    },
    {
        persist: true
    }
)
